package com.scvtc.service.student.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scvtc.domain.ResResult;
import com.scvtc.domain.dto.student.award.StudentAwardDto;
import com.scvtc.domain.entity.student.StudentAwards;
import com.scvtc.domain.vo.PageVo;
import com.scvtc.domain.vo.student.award.StudentAwardVo;
import com.scvtc.mapper.student.StudentAwardsMapper;
import com.scvtc.service.student.StudentAwardsService;
import com.scvtc.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 学生获奖信息(StudentAwards)表服务实现类
 *
 * @author yiFei
 * @since 2022-04-13 17:38:27
 */
@Service("studentAwardsService")
public class StudentAwardsServiceImpl extends ServiceImpl<StudentAwardsMapper, StudentAwards> implements StudentAwardsService {


//    @Autowired
//    private StudentsBaseService studentsBaseService;

    /**
     * 查询学生获奖信息
     *
     * @param current
     * @param size
     * @param studentAwardVo
     * @return
     */
    @Override
    public ResResult<PageVo<List<StudentAwardVo>>> getAllStudentAwards(Long current, Long size, StudentAwardVo studentAwardVo) {
        // 分页查询
        Page<StudentAwards> page = getBaseMapper().getAllStudentAwards(new Page<StudentAwards>(current, size), studentAwardVo);
        // Bean拷贝
        List<StudentAwardVo> studentAwardVos = BeanCopyUtils.copyBean(page.getRecords(), StudentAwardVo.class);
        // 返回分页信息
        return ResResult.noMsgSuccess(new PageVo<>(studentAwardVos, page.getTotal()));
    }

    /**
     * 添加单个学生获奖信息
     *
     * @param studentAwardDto
     * @return
     */
    @Override
    public ResResult<?> addStudentAwards(StudentAwardDto studentAwardDto) {
        // 如果考虑安全性 可以放开这段代码
//        StudentsBase one = studentsBaseService.getOne(
//                new LambdaQueryWrapper<StudentsBase>()
//                        .eq(!ObjectUtils.isEmpty(studentAwardDto.getId()), StudentsBase::getId, studentAwardDto.getStuId())
//                        .eq(StringUtils.hasText(studentAwardDto.getStuNum()), StudentsBase::getStuNum, studentAwardDto.getStuNum())
//        );
//        // 如果没有查询到数据 (返回学生信息错误)
//        if (ObjectUtils.isEmpty(one)) {
//            return ResResult.customize(STUDENT_DOES_NOT_EXIST);
//        }
        StudentAwards studentAwards = BeanCopyUtils.copyBean(studentAwardDto, StudentAwards.class).setId(null);
        if (save(studentAwards)) {
            return ResResult.success(studentAwards.getId());
        }
        return ResResult.fail();
    }

    /**
     * 修改单个学生获奖信息 （ 只能修改对应学生获奖信息编号 ， 不能修改学生获奖信息信息）
     *
     * @param studentAwardDto
     * @return
     */
    @Override
    public ResResult<?> updateStudentAwards(StudentAwardDto studentAwardDto) {
        // 查询学生 id 和 学生编号匹配的数据 (考虑安全性就需要放开这段代码)
//        StudentsBase one = studentsBaseService.getOne(
//                new LambdaQueryWrapper<StudentsBase>()
//                        .eq(!ObjectUtils.isEmpty(studentAwardDto.getId()), StudentsBase::getId, studentAwardDto.getStuId())
//                        .eq(StringUtils.hasText(studentAwardDto.getStuNum()), StudentsBase::getStuNum, studentAwardDto.getStuNum())
//        );
//        // 如果没有查询到数据 (返回学生信息错误)
//        if (ObjectUtils.isEmpty(one)) {
//            return ResResult.customize(STUDENT_DOES_NOT_EXIST);
//        }
        // 拷贝
        StudentAwards studentAwards = BeanCopyUtils.copyBean(studentAwardDto, StudentAwards.class);
        // 判断是否插入成功
        if (updateById(studentAwards)) {
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 删除单个学生获奖信息
     *
     * @param id
     * @return
     */
    @Override
    public ResResult<?> deleteStudentAwards(Long id) {
        if (removeById(id)) {
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 删除多个学生获奖信息
     *
     * @param ids
     * @return
     */
    @Override
    public ResResult<?> deleteMultipleStudentAwards(Long[] ids) {
        if (removeByIds(Arrays.asList(ids))) {
            return ResResult.success();
        }
        return ResResult.fail();
    }
}

